﻿using BaseData;
using BaseData.Exts;
using BaseData.Models;
using BaseLib.Helper;
using BaseUtility;
using System;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Web;

namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// GroupAdminInfoForm 的摘要说明
    /// </summary>
    public class GroupAdminInfoForm : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            Response.Write(Msg(context).ObjectsToJson());
            Response.ContentType = "text/plain";
        }

        private static MsgErr Msg(HttpContext context)
        {
            var actstr = string.Empty;
            var infoInit = new Web_Base_News_Info();
            try
            {
                var action = (Enums.Action)Enum.Parse(typeof(Enums.Action), Request.QueryString["action"] ?? Request.Form["action"]);
                switch (action)
                {
                    case Enums.Action.ActNew:
                        actstr = "新建";
                        break;
                    case Enums.Action.ActEdit:
                        actstr = "修改";
                        break;
                    case Enums.Action.ActStop:
                        actstr = "停止";
                        break;
                    case Enums.Action.ActView:
                        actstr = "查看";
                        break;
                    case Enums.Action.ActDelete:
                        actstr = "删除";
                        break;
                    case Enums.Action.ActReply:
                        actstr = "回复";
                        break;
                    default:
                        return new MsgErr { error = 1, action = actstr, message = new StringBuilder("未知的操作方式！"), };
                }
                if (!Request.HttpMethod.Compare("POST", true)) return new MsgErr { error = 1, action = actstr, message = new StringBuilder("不支持Get方式！"), };
                var id = Request.QueryString["id"].ToInt(0);
                var infoInitParent = new Web_Base_News_Info();
                var tempInit = new Web_Base_News_Info()
                {
                    Title = Request.Form["Title"].ToNull(),
                    Content = Request.Form["Content"].ToNull(),
                    TitleExt = Request.Form["TitleExt"].ToNull(),
                    ContentExt = Request.Form["ContentExt"].ToNull(),
                    // FlagView = Request.Form["FlagView"].ToInt(),
                    // FlagPass = Request.Form["FlagPass"].ToInt(),
                    // FlagStatus = Request.Form["FlagStatus"].ToInt(),
                };
                using (var ctx = new ContextExt())
                {
                    switch (action)
                    {
                        case Enums.Action.ActNew:
                            infoInit = new Web_Base_News_Info()
                            {
                                ParentId = tempInit.ParentId,
                                Title = tempInit.Title,
                                Content = tempInit.Content,
                                // DateTimeCreate = System.DateTime.Now,
                                // FlagPass = tempInit.FlagPass,
                                // FlagStatus = tempInit.FlagStatus,
                                // FlagView = tempInit.FlagView,
                            };
                            break;
                        case Enums.Action.ActReply:
                            var tempReplies1 = ctx.Web_Base_News_Info.Where(q => q.PkId == id);
                            if (!tempReplies1.Any()) return new MsgErr { error = 1, action = actstr, message = new StringBuilder("未找到要回复的记录！"), };
                            infoInit = tempReplies1.FirstOrDefault();
                            if (infoInit != null)
                            {
                                infoInit.TitleExt = tempInit.TitleExt;
                                infoInit.ContentExt = tempInit.ContentExt;
                                // infoInit.FlagPass = tempInit.FlagPass;
                                // infoInit.FlagStatus = tempInit.FlagStatus;
                                // infoInit.FlagView = tempInit.FlagView;
                                // infoInit.DateTimePass = tempInit.DateTimePass;
                            }
                            break;
                        case Enums.Action.ActEdit:
                            var tempEdit = ctx.Web_Base_News_Info.Where(q => q.PkId == id);
                            if (!tempEdit.Any()) return new MsgErr { error = 1, action = actstr, message = new StringBuilder("未找到要修改的记录！"), };
                            infoInit = tempEdit.FirstOrDefault();
                            if (infoInit != null)
                            {
                                infoInit.Title = tempInit.Title;
                                infoInit.Content = tempInit.Content;
                                // infoInit.FlagView = tempInit.FlagView;
                                // infoInit.FlagPass = tempInit.FlagPass;
                                // infoInit.FlagStatus = tempInit.FlagStatus;
                            }
                            break;
                        case Enums.Action.Default:
                            break;
                    }
                    ctx.Web_Base_News_Info.AddOrUpdate(m => m.PkId, infoInit);
                    ctx.SaveChanges();
                }
                return new MsgErr { error = 0, action = actstr, message = new StringBuilder("操作成功！"), };
            }
            catch (Exception err)
            {
                return new MsgErr { error = 1, action = actstr, message = new StringBuilder(value: err.Message), };
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}